import 'package:flutter/material.dart';
import 'package:wsc2022/home.dart';

import 'TabBar/TabView1.dart';
import 'TabBar/TabView2.dart';
import 'TabBar/TabView3.dart';

void main() => runApp(const MyApp());

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> with SingleTickerProviderStateMixin {
  late TabController _tabController;

  var _isDarkMode = false;
  var _switchValue = false;

  @override
  void initState() {
    _tabController = TabController(
      initialIndex: 0,
      length: 3,
      vsync: this,
    );
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Material App',
      theme: _isDarkMode
          ? ThemeData.dark(useMaterial3: true)
          : ThemeData.light(useMaterial3: true)
              .copyWith(primaryColor: Color(0xFFEEEEEE)),
      home: Scaffold(
        body: NestedScrollView(
          floatHeaderSlivers: true,
          headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
            return <Widget>[
              SliverAppBar(
                // backgroundColor: Colors.transparent,
                actions: [
                  IconButton(
                    onPressed: () => showModeSwitch(context),
                    icon: Icon(Icons.settings),
                  ),
                ],
                flexibleSpace: Container(
                  decoration: BoxDecoration(
                    color: ThemeData.dark().primaryColor,
                    image: DecorationImage(
                      image: AssetImage('images/bgc1.png'),
                      fit: BoxFit.cover,
                    ),
                  ),
                ),
                expandedHeight: 300,
                toolbarHeight: 100,
                // centerTitle: true,
                pinned: true,
                // floating: true,
                bottom: TabBar(
                    indicatorColor: Colors.white,
                    labelPadding: const EdgeInsets.only(
                      bottom: 16,
                    ),
                    controller: _tabController,
                    tabs: [
                      Text("News"),
                      Text("Results"),
                      Text("History"),
                    ]),
              ),
            ];
          },
          body: TabBarView(
            controller: _tabController,
            children: [TabView1(), TabView2(), TabView3()],
          ),
        ),
      ),
    );
  }

  void showModeSwitch(BuildContext _context) {
    showModalBottomSheet(
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
      context: _context,
      builder: (BuildContext context) {
        return Container(
          decoration: BoxDecoration(
            borderRadius: BorderRadius.only(
              topLeft: Radius.circular(20.0),
            ),
          ),
          clipBehavior: Clip.antiAlias,
          height: 160,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Padding(
                padding: const EdgeInsets.all(20.0),
                child: Text(
                  "Settings",
                  style: Theme.of(_context).textTheme.titleLarge,
                ),
              ),
              SwitchListTile(
                title: Text(!_switchValue ? "Dark Mode" : "Light Mode"),
                value: _switchValue,
                onChanged: (a) {
                  _switchValue = a;
                  _isDarkMode = !_isDarkMode;
                  setState(() {});
                },
              ),
            ],
          ),
        );
      },
    );
  }
}
